"""响应处理工具

提供统一的API响应格式化功能，确保所有API返回一致的响应结构。
"""

import uuid
from typing import Any, Dict, Optional
from flask import jsonify

def create_response(data: Any = None, 
                  success: bool = True, 
                  error_code: Optional[str] = None, 
                  error_message: Optional[str] = None, 
                  status_code: int = 200) -> tuple:
    """创建标准化的API响应
    
    Args:
        data: 响应数据
        success: 请求是否成功
        error_code: 错误代码
        error_message: 错误消息
        status_code: HTTP状态码
        
    Returns:
        包含标准化响应字典和状态码的元组
    """
    response = {
        'success': success,
        'data': data,
        'requestId': str(uuid.uuid4())
    }
    
    if not success:
        response['error'] = {
            'code': error_code or 'UNKNOWN_ERROR',
            'message': error_message or 'An unknown error occurred'
        }
    
    return response, status_code